/**
 * Created by ACER on 2017/4/13.
 */
app.controller('history-task', function ($scope, $http) {
    var token = localStorage.getItem('token');


    if (!token) {
        $.alert('请先登录哟', function () {
            location.href = '#login';
        });
        return;
    }


    $scope.page = 1;
    //获取主页面任务id


    $scope.AllHistoryInfo = [];
    $scope.historyInfo = {};
    $scope.getHistoryInfo = function (page) {

        $scope.historyInfo.page = page ? page : 1;
        if (!page) {
            $scope.page = 1;
            $scope.AllHistoryInfo = [];
        }
        $.showPreloader('加载中...');
        $http({
            method: 'get',
            url: app.netPath + '/tasks/details/show',
            headers: {Authorization: token},
            params: $scope.historyInfo
        }).then(function (res) {
            if (!res.data.error) {

                var AllHistoryInfo = res.data.data;
                for (var i = 0; i < AllHistoryInfo.length; i++) {
                    AllHistoryInfo[i].isPreDelete = false;
                    $scope.AllHistoryInfo.push(AllHistoryInfo[i]);
                }


                var infiniteScroll = $('.infinite-scroll');
                var infiniteScrollPreloader = $('.infinite-scroll-preloader');
                // 启用无限加载事件
                $.attachInfiniteScroll(infiniteScroll);
                // 启用提示符
                infiniteScrollPreloader.show();

                if (res.data.allPage <= $scope.historyInfo.page) {
                    // 加载完毕，则注销无限加载事件，以防不必要的加载
                    $.detachInfiniteScroll(infiniteScroll);
                    // 删除加载提示符
                    infiniteScrollPreloader.hide();
                }

            }

            else {
                var errorCode = res.data.error;
                switch (errorCode) {
                    case  200:
                        $.alert('查询失败');
                        break;
                    case  110:
                        $.alert('你还没有发布任务');
                        break;

                }
            }
            $scope.isOnloading = false;
            $.hidePreloader();

        }, function () {
            $.alert('网络错误');
            $.hidePreloader();
            $scope.isOnloading = false;
            $.hidePreloader();

        })
    };

    //获取taskID
    $scope.getTaskId = function (taskid) {
        sessionStorage.setItem('taskid', taskid);
        location.href = '#personal-task-details';
    };

    // 删除所有选定任务
    $scope.deleteTaskSelected = function () {
        if ($scope.AllHistoryInfo.length == 0) {
            return;
        }
        var taskSelected = [];
        for (var i = 0; i < $scope.AllHistoryInfo.length; i++) {
            console.log($scope.AllHistoryInfo[i].isPreDelete)
            if ($scope.AllHistoryInfo[i].isPreDelete) {
                taskSelected.push({
                    taskId: $scope.AllHistoryInfo[i].taskId
                });
            }
        }
        if (taskSelected.length == 0) {
            $.alert('你没有选中任何任务');
            return;
        }
        $.confirm('删除所有选择的任务？', function () {
            $http({
                method: 'post',
                url: app.netPath + '/tasks/details/someTaskDelete',
                data: taskSelected,
                headers: {Authorization: token}
            }).then(function (res) {
                if (!res.data.error) {
                    $scope.getHistoryInfo();
                    $.alert('删除成功');
                }
                else {
                    var errorCode = res.data.error;
                    switch (errorCode) {
                        case  202:
                            $.alert('删除失败，请重试');
                    }
                }
            }, function () {
                $.alert('网络走丢了');
            });
        });
    };

//删除任务
    $scope.deleteTask = function (taskid) {
        $http({
            method: 'post',
            url: app.netPath + '/tasks/details/delete',
            headers: {Authorization: token},
            params: {taskId: taskid}
        }).then(function (res) {
            if (!res.data.error) {
                $scope.getHistoryInfo();
                $.alert('删除成功');
            }
            else {

                var errorCode = res.data.error;
                switch (errorCode) {
                    case  202:
                        return '删除失败，请重试';

                }
            }
        }, function () {
            $.alert('网络走丢了');
        })
    };

});